<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-tab-content" style="height: auto">
                <form class="layui-form" lay-filter="component-form-element" action="">
                    <input type="hidden" name="selectTid" id="selectTid">
                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">代码类别值</label>
                                <div class="layui-input-block">
                                    <input type="text" name="selectName" lay-verify="selectName" id="selectName"
                                           autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                        </div>
                    </div>
<%--                    <div class="layui-form-item">--%>
<%--                        <div class="layui-col-lg6">--%>
<%--                            <div class="layui-col-xs11">--%>
<%--                                <label class="layui-form-label">代码值</label>--%>
<%--                                <div class="layui-input-block">--%>
<%--                                    <input type="text" name="optionCode" lay-verify="optionCode" id="optionCode"--%>
<%--                                           autocomplete="off" placeholder="请输入代码值" class="layui-input">--%>
<%--                                </div>--%>
<%--                            </div>--%>
<%--                            <div class="layui-col-xs1">--%>
<%--                                <div class="layui-form-mid layui-word-aux"--%>
<%--                                     style="color: red !important;padding: 10px !important;">*--%>
<%--                                </div>--%>
<%--                            </div>--%>
<%--                        </div>--%>
<%--                    </div>--%>
                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">代码值名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="optionName" lay-verify="optionName" id="optionName"
                                           autocomplete="off" placeholder="请输入代码值名称" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-xs1">
                                <div class="layui-form-mid layui-word-aux"
                                     style="color: red !important;padding: 10px !important;">*
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-col-lg6">
                            <div class="layui-col-xs11">
                                <label class="layui-form-label">创建时间</label>
                                <div class="layui-input-block">
                                    <input name="createTime" id="createTime" lay-verify="createTime" autocomplete="off"
                                           class="layui-input" type="text">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0;padding: 0px;">
                                <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">立即提交</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script>
    function child(id) {
        layui.use(['form', 'laydate'], function () {
            var $ = layui.$
                , form = layui.form
                , laydate = layui.laydate,
                layer = layui.layer;

            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var date = date.getDate();
            var now = year + '-' + p(month) + "-" + p(date);

            function p(s) {
                return s < 10 ? '0' + s : s;
            }

            laydate.render({
                elem: "#createTime",
                btns: ['now', 'confirm'],
                theme: 'molv',
                value: now,
                calendar: true
            });

            $("#selectTid").val(id);

            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/getOneStructure",
                dataType: "json",
                data: "id=" + id,
                async: false,
                success: function (data) {
                    $("#selectName").val(data.data[0].name);
                }
            })

            form.verify({
                <%--optionCode: function (value, item) {--%>
                <%--    if (value == "") {--%>
                <%--        return '代码值必填';--%>
                <%--    } else {--%>
                <%--        var flag = true;--%>
                <%--        $.ajax({--%>
                <%--            type: "post",--%>
                <%--            url: "<%=request.getContextPath()%>/rest/system/getCodeExsit",--%>
                <%--            dataType: "json",--%>
                <%--            async: false,--%>
                <%--            data: {"key": "optionCode", "value": value, "id": value},--%>
                <%--            success: function (data) {--%>
                <%--                if (data == false) {--%>
                <%--                    flag = false;--%>
                <%--                }--%>
                <%--            }--%>
                <%--        })--%>

                <%--        if (flag == false) {--%>
                <%--            return "代码值已存在"--%>
                <%--        }--%>
                <%--    }--%>
                <%--},--%>
                optionName: function (value, item) {
                    if (value == "") {
                        return '代码值名称必填';
                    } else {
                        var flag = true;
                        $.ajax({
                            type: "post",
                            url: "<%=request.getContextPath()%>/rest/system/getCodeExsit",
                            dataType: "json",
                            async: false,
                            data: {"key": "optionName", "value": value, "id": id},
                            success: function (data) {
                                if (data == false) {
                                    flag = false;
                                }
                            }
                        })
                        if (flag == false) {
                            return "代码值名称已存在"
                        }
                    }
                },
                createTime: function (value, item) {
                    if (value == "") {
                        return '创建时间必填';
                    }
                }
            });

            form.on('submit(*)', function (data) {
                var field = data.field
                var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
                $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/system/addCode",
                    dataType: "json",
                    async: false,
                    data: field,
                    success: function (msg) {
                        setTimeout(function () {
                            layer.close(index);
                            if (msg == true) {
                                layer.msg('添加成功');
                            } else {
                                layer.msg(msg.errorMsg);
                            }
                        }, 2000);
                        setTimeout(function () {
                            var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                            parent.layer.close(indexs);//关闭弹出层
                        }, 4000);
                    }
                })
                return false;
            })

        });
    }
</script>
</body>
</html>